//
//  KDReportCenterController.m
//  WangJingApp
//
//  Created by 魏强 on 17/11/10.
//  Copyright © 2017年 KDTech. All rights reserved.
//

#import "KDReportCenterController.h"
#import "KDChildReportCenterController.h"
#import "KDReportListCell.h"
#import "KDReportController.h"
#import "KDReportRequestTool.h"
#import "KDBaseListParam.h"
#import "KDTabBarController.h"
#import "UITableView+FDTemplateLayoutCell.h"
#import "KDReportInfoController.h"
@interface KDReportCenterController ()<UITableViewDelegate,UITableViewDataSource,KDReportListCellDelegate>
/**
 *  所有主题数据的集合
 */
@property (nonatomic, strong) NSArray * themeArr;
/**  <#Description#> */
@property (nonatomic, strong) NSArray *infoArr;
/**  <#Description#> */
@property (nonatomic, weak) UITableView *currentTabV;
/**
 *  加载第几页
 */
@property (nonatomic, copy) NSString *page;
@end

@implementation KDReportCenterController
#pragma mark - 懒加载区
- (NSString *)page
{
    if (!_page) {
        _page = @"1";
    }
    return _page;
}
- (NSArray *)infoArr
{
    if (!_infoArr) {
        _infoArr = [NSArray array];
    }
    return _infoArr;
}
- (NSArray *)themeArr
{
    if (!_themeArr) {
        _themeArr = @[@"公安网",@"91"] ;
    }
    return _themeArr;
}
#pragma mark - 生命周期区
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupNavBar];
     [self setUpTableView];
//    [self setupAllChildViewController];
    // Do any additional setup after loading the view.
}
#pragma mark - 板块布置区
- (void)setUpTableView
{
    UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0  , 0, screenW, self.view.height -64) style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.tag = 1000;
    tableView.tableFooterView = [UIView new];
    tableView.separatorColor = BackGroundColor;
    tableView.backgroundColor = BackGroundColor;
    tableView.rowHeight = Cell_h;
    if (!iOS7) {
        tableView.estimatedRowHeight = Cell_h;
    }
    self.currentTabV = tableView;
    [self.view addSubview:tableView];
    UINib *xib =[UINib nibWithNibName:@"KDReportListCell" bundle:nil] ;
    
    [tableView registerNib:xib forCellReuseIdentifier:@"KDReportListCell"];
    tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadDetailData)];
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreDetailData)];
    [footer setTitle:@"" forState:MJRefreshStateIdle];
    // 设置footer
    self.currentTabV.footer = footer;
    [self.currentTabV.header beginRefreshing];
}
- (void)setupNavBar{
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"collect_add"] style:UIBarButtonItemStylePlain target:self action:@selector(rightItemClick)];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"leftArrow"] style:UIBarButtonItemStylePlain target:self action:@selector(leftItemClick)];
    
}
//- (void)setupAllChildViewController{
//    if (self.childViewControllers.count) {
//        for (UIViewController *child in self.childViewControllers) {
//            [child removeFromParentViewController];
//            [child.view removeFromSuperview];
//        }
//    }
//    for (NSString *name in self.themeArr) {
//        
//        KDChildReportCenterController *child = [[KDChildReportCenterController alloc]init];
//        child.title = name;
////        child.view.tag = ;
//        [self addChildViewController:child];
//        //        }
//    }
//    // 4.设置所有标题
//    self.needUnderLine = YES;
//    [super setupManageBtn];
//}

#pragma mark - 事件点击区
- (void)leftItemClick{
    [self dismissViewControllerAnimated:YES completion:nil];
}
//- (void)rightItemClick{
////    [self dismissViewControllerAnimated:YES completion:nil];
//}
#pragma mark - 网络请求区
- (void)loadDetailData{
    WS(ws);
    KDBaseListParam *param = [[KDBaseListParam alloc]init];
    param.limit = @"20";
    self.page = @"1";
    param.page = self.page;
    [KDReportRequestTool getReportListWithParam:param success:^(KDBaseResult *responseObejct) {
        if ([responseObejct.code isEqualToString:@"1"]) {
            ws.infoArr = [KDReportListCellModel objectArrayWithKeyValuesArray:responseObejct.data[@"data"]];
            [ws.currentTabV.header endRefreshing];
            [ws.currentTabV reloadData];

        }else if ([responseObejct.code isEqualToString:@"E040110000001"]){
            [ws.currentTabV.header endRefreshing];
            [ws.navigationController dismissViewControllerAnimated:NO completion:^{
                KDNewfeatureController *new = [[KDNewfeatureController alloc]init];
                __block KDTabBarController*ctr = (KDTabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
                [UIApplication sharedApplication].keyWindow.rootViewController = new;
                [ctr removeObserver];
                [ctr removeFromParentViewController];
                ctr = nil;
            }];
        }else{
            [SVProgressHUD showErrorWithStatus:responseObejct.msg maskType:SVProgressHUDMaskTypeGradient];
            [ws.currentTabV.header endRefreshing];
        }
        
    } failure:^(NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"网络错误！" maskType:SVProgressHUDMaskTypeGradient];
        [ws.currentTabV.header endRefreshing];
        KDLog(@"%@",error);
        
    }];
}
- (void)loadMoreDetailData{
    WS(ws);
    KDBaseListParam *param = [[KDBaseListParam alloc]init];
    self.page = [NSString stringWithFormat:@"%ld", self.page.integerValue +1];
    param.page = self.page;
    
    param.limit = @"20";
    [KDReportRequestTool getReportListWithParam:param success:^(KDBaseResult *responseObejct) {
        if ([responseObejct.code isEqualToString:@"1"]) {
            NSArray *arr1 = [KDReportListCellModel objectArrayWithKeyValuesArray:responseObejct.data[@"data"]];
            if (arr1.count) {
                NSMutableArray *arr = [NSMutableArray arrayWithArray:ws.infoArr];
                [arr addObjectsFromArray:arr1];
                ws.infoArr = arr;
                [ws.currentTabV.footer endRefreshing];
                [ws.currentTabV reloadData];
            }else{
                [ws.currentTabV.footer noticeNoMoreData];
            }
            
            
            [ws.currentTabV reloadData];
        }else if ([responseObejct.code isEqualToString:@"E040110000001"]){
            [ws.currentTabV.footer endRefreshing];
            [ws.navigationController dismissViewControllerAnimated:NO completion:^{
                KDNewfeatureController *new = [[KDNewfeatureController alloc]init];
                __block KDTabBarController*ctr = (KDTabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
                [UIApplication sharedApplication].keyWindow.rootViewController = new;
                [ctr removeObserver];
                [ctr removeFromParentViewController];
                ctr = nil;
            }];
        }else{
            [SVProgressHUD showErrorWithStatus:responseObejct.msg maskType:SVProgressHUDMaskTypeGradient];
            [ws.currentTabV.footer endRefreshing];
        }
        
    } failure:^(NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"网络错误！" maskType:SVProgressHUDMaskTypeGradient];
        [ws.currentTabV.footer endRefreshing];
        KDLog(@"%@",error);
        
    }];
}
#pragma mark - 通知接收区
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.infoArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    KDReportListCell *cell = [KDReportListCell cellWithTableView:tableView];
    //    KDLog(@"waimian%d", indexPath.row);
    cell.delegate = self;
    if (tableView == self.currentTabV)
    {
        cell.delegate = self;
        if (self.infoArr.count != 0)
        {
            cell.model = self.infoArr[indexPath.row];
        }
        return cell;
    }else{
        return cell;
    }
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [tableView fd_heightForCellWithIdentifier:@"KDReportListCell" configuration:^(KDReportListCell * cell) {
        cell.model = self.infoArr[indexPath.row];
    }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.currentTabV) {
        if (self.infoArr.count == 0) {
            return;
        }
        KDReportListCell *cell = (KDReportListCell *)[tableView cellForRowAtIndexPath:indexPath];
        NSMutableArray *arr = [NSMutableArray arrayWithArray:self.infoArr];
        [arr replaceObjectAtIndex:indexPath.row withObject:cell.model];
        KDReportInfoController *ctr = [[KDReportInfoController alloc]init];
        
        ctr.model = cell.model;
        [self.navigationController pushViewController:ctr animated:YES];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - KDReportListCellDelegate
- (void)reportListCell:(KDReportListCell *)cell reportBtnClick:(UIButton *)reportBtn{
    KDReportListCellModel *model = cell.model;
    
    KDReportController *reportCtrl = [[KDReportController alloc]init];
//    KDPatrolgetReportParam *param = [[KDPatrolgetReportParam alloc]init];
//    param.url = model.url;
//    param.info_title = model.info_title;
//    param.info_content = model.info_content;
//    param.website_type = model.website_type;
//    param.website_name = model.website_name;
    model.ID = 0;
    reportCtrl.param = model;
    reportCtrl.type = 1;
//    reportCtrl.delegate = self;
    [self.navigationController pushViewController:reportCtrl animated:YES];
}

@end
